#include<hikari.h>
#include<stdint.h>

std::string Utils::wstring_to_string(const std::wstring& text){
    std::locale const loc("");
    wchar_t const* from = text.c_str();
    std::size_t const len = text.size();
    std::vector<char> buffer(len + 1);
    std::use_facet<std::ctype<wchar_t> >(loc).narrow(from, from + len, '_', &buffer[0]);
    return std::string(&buffer[0], &buffer[len]);
}

std::wstring Utils::string_to_wstring(std::string& text){
	return std::wstring( text.begin(), text.end() );
}

bool Utils::equal(std::wstring&str1,std:: wstring&str2){
	return str1.compare(str2)==0;
}

bool Utils::equal(std::wstring&str1, const wchar_t*str2){
	return str1.compare(str2)==0;
}

bool Utils::isEmpty(std::wstring&string){
	return equal(string,L"");
}

long long Utils::doubleHash(double x){
	uint64_t bits;
    memcpy(&bits, &x, sizeof bits);
    return (long long)bits;
}

long Utils::hash_WString(const std::wstring&s){
	long hash = 0;
	int offset = 'a' - 1;
	const wchar_t*val=s.c_str();
	int length=s.length();
	for(int i=0;i<length;i++) {
		hash = 31*hash + val[i];
	}
	return hash;
}

long Utils::hash_WString(const wchar_t*name){
	long hash = 0;
	int offset = 'a' - 1;
	
	const wchar_t*val=name;
	int length=wcslen(name);
	for(int i=0;i<length;i++) {
		hash = 31*hash + val[i];
	}
	return hash;
}

int Utils::lastIndexOf(std::wstring&string, wchar_t symbol){
	const wchar_t*buffer=string.c_str();
	for(int i=string.length()-1;i>=0;i--){
		if(buffer[i]==symbol)return i;
	}
	return -1;
}


std::wstring Utils::replaceWString(std::wstring&string, wchar_t toFind, wchar_t toReplace){
	int index=lastIndexOf(string, toFind);
	if(index==-1){
		return string;
	}else{
		wchar_t*data=(wchar_t*)string.c_str();
		int length=string.length();
		for(int i=0;i<length;i++){
			if(data[i]==toFind){
				data[i]=toReplace;
			}
		}
		return std::wstring(data);
	}
}

std::wstring Utils::getFileExtension(std::wstring&path){
	int index=lastIndexOf(path, '.');
	if(index!=-1){
		return path.substr(index, path.length()-index);
	}
	return std::wstring(L"");
}

std::wstring Utils::getFilePath(std::wstring&path){
	int index=lastIndexOf(path, '\\');
	if(index==-1){
		index=lastIndexOf(path, '/');
	}
	if(index!=-1){
		return path.substr(0, index+1);
	}
	return std::wstring(L"");
}

std::wstring Utils::getFileWithExtension(std::wstring&path){
	int index=lastIndexOf(path, '\\');
	if(index==-1){
		index=lastIndexOf(path, '/');
	}
	if(index!=-1){
		return path.substr(index+1);
	}
	return std::wstring(L"");
}

std::wstring Utils::getFileNameWithoutExtension(std::wstring&path){
	std::wstring fileName=getFileWithExtension(path);

	int index=lastIndexOf(fileName, '.');
	if(index!=-1){
		return fileName.substr(0, index);
	}
	return std::wstring(L"");
}

void Utils::errorLog(std::wstring&message){
	wprintf(L"%s\n", message.c_str());
}

void Utils::errorLog(const wchar_t*message){
	wprintf(L"%s\n", message);
}

std::wstring Utils::wstring(int value){
	wchar_t buffer[50];
	swprintf(buffer,L"%d", value);
	return std::wstring(buffer);
}

/*btVector3 Utils::vectorToBtVector(const irr::core::vector3df&vector){
	return btVector3(vector.X, vector.Y, vector.Z);
}

irr::core::vector3df Utils::btVectorToVector(const btVector3&v){
	return irr::core::vector3df(v.getX(), v.getY(), v.getZ());
}*/